package com.jsecode.lib.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    /**
     * 获得文件MD5校验码
     * 
     * @param path
     * @return
     */
    public static String getFileMD5(String path) {
        String md5Str = null;
        FileInputStream fis = null;
        ByteArrayOutputStream out = null;
        try {
            File f = new File(path);
            if (f.exists()) {
                fis = new FileInputStream(f);
                out = new ByteArrayOutputStream();
                int length = 8192;
                byte[] datas = new byte[length];
                while ((length = fis.read(datas, 0, length)) != -1) {
                    out.write(datas, 0, length);
                }
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] bs = md.digest(out.toByteArray());
                int i;
                StringBuffer buf = new StringBuffer("");
                for (int offset = 0; offset < bs.length; offset++) {
                    i = bs[offset];
                    if (i < 0)
                        i += 256;
                    if (i < 16)
                        buf.append("0");
                    buf.append(Integer.toHexString(i));
                }
                md5Str = buf.toString().toUpperCase();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return md5Str;
    }

    
    /**
     * 获得字符串的MD5码
     * 
     * @param str
     * @return
     */
    public static String getStringMd5(String str) {
        String md5Str = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bs = md.digest(str.getBytes());
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < bs.length; offset++) {
                i = bs[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            md5Str = buf.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5Str;
    }
    
    /**
     * 获得字符串的MD5码, 返回加密后的字节数组
     * 
     * @param str
     * @return
     */
    public static byte[] getMd5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return md.digest(str.getBytes());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
